package com.twq.TestThread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.net.URL;

//练习Thread，实习多线程同步下载图片
public class TestThread2 extends Thread{
    private  String url;
    private  String name;

    public TestThread2(String url, String name){
        this.name = name;
        this.url = url;
    }

    //下载图片的执行体
    @Override
    public void run(){
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载文件名为"+name);
    }

    public static void main(String[] args) {
       TestThread2 t1 = new TestThread2("https://img11.51tietu.net/pic/2016-071418/20160714181543xyu10ukncwf221991.jpg","1.jpg");
        t1.start();
        TestThread2 t2 = new TestThread2("https://img1.baidu.com/it/u=3886212450,854269223&fm=26&fmt=auto&gp=0.jpg","2.jpg");
        t2.start();
    }

    //下载器
    class WebDownloader{
        //下载方法
        public  void downloader(String url,String name){
            try {
                FileUtils.copyURLToFile(new URL(url),new File(name));
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("io异常，download方法出现问题");
            }

        }
    }
}
